package com.jrx.anytxn.param.controller.investor;

import com.google.common.collect.Maps;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.investor.FundTableReq;
import com.jrx.anytxn.param.dto.investor.FundTableRes;
import com.jrx.anytxn.param.dto.investor.PrInvestorTableRes;
import com.jrx.anytxn.param.entity.PrFundTable;
import com.jrx.anytxn.param.service.investor.IFundTableService;

import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 资金源表
 *
 * @author 黄秋平
 * @date 2020-5-14 13:39:08
 */
@Api(tags = "资金源参数api接口")
@RestController
@RequestMapping(value = "/param/investor/funds")
public class FundTableController {

    private static final Logger logger= LoggerFactory.getLogger(FundTableController.class);

    @Resource
    private IFundTableService fundTableService;

    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID分页查询资金源表信息",notes = "根据租户ID分页查询资金源表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="页大小",required = true)})
    public TxnRespResult<TxnPage<FundTableRes>> getAllPageByTenId(@PathVariable(value = "pageNum") Integer pageNum,@PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<FundTableRes>> result = new TxnRespResult<>();
        TxnPage<FundTableRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = fundTableService.findAllPageByTenId(pageNum,pageSize,tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
       return result;
    }

    @ApiOperation(value = "新增资金源表信息",notes = "新增资金源表信息")
    @PostMapping("/")
    public TxnRespResult<FundTableRes> createFundTable(@RequestBody FundTableReq fundTableReq) {
        TxnRespResult<FundTableRes> result = new TxnRespResult<>();
        FundTableRes response;
        try {
            fundTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            fundTableReq.setUpdateBy(infoService.getUser().getUserName());
            response = fundTableService.addFundTable(fundTableReq);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑资金源表信息",notes = "编辑资金源表信息")
    @PutMapping("/")
    public TxnRespResult<FundTableRes> modifyFundTable(@RequestBody FundTableReq fundTableReq) {
        TxnRespResult<FundTableRes> result = new TxnRespResult<>();
        FundTableRes res;
        try {
            fundTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = fundTableService.modifyFundTable(fundTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和资金源ID删除资金源表信息",notes = "根据租户ID和资金源ID删除资金源表信息")
    @DeleteMapping(value = "/{tenantId}/{fundId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId",value = "租户ID",required = true),
            @ApiImplicitParam(name = "fundId",value = "资金源ID",required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndFundId(@PathVariable("tenantId") String tenantId,@PathVariable("fundId") String fundId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);
            flag = fundTableService.removeByFundIdAndTenId(tenantId, fundId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和资金源表ID查询资金源表信息", notes = "根据租户ID和资金源表ID查询资金源表信息")
    @GetMapping(value = "/{tenantId}/{fundId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId",value = "租户ID",required = true),
            @ApiImplicitParam(name = "fundId",value = "资金源ID",required = true)
    })
    public TxnRespResult<FundTableRes> getByTenIdAndFundId(@PathVariable("tenantId") String tenantId,@PathVariable("fundId") String fundId) {
        TxnRespResult<FundTableRes> result = new TxnRespResult<>();
        FundTableRes res;
        try {
            res = fundTableService.findByFundIdAndTenId(tenantId, fundId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "获得资金源id下拉信息", notes = "获得资金源id下拉信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<PrFundTable>>> getInvestorInfoList() {
        TxnRespResult<HashMap<String, List<PrFundTable>>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<PrFundTable> allPartner = fundTableService.findAllByTenId(tenantId);
        HashMap<String, List<PrFundTable>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", allPartner);
        return result.getSuccess(sysCodeMap);
    }


    @ApiOperation(value = "根据资金源表ID查询对应出资方相关信息", notes = "根据资金源表ID查询对应出资方相关信息")
    @GetMapping(value = "/{fundId}")
    @ApiImplicitParam(name = "fundId",value = "资金源ID",required = true)
    public TxnRespResult<List<PrInvestorTableRes>> getByTenIdAndFundId(@PathVariable("fundId") String fundId) {
        TxnRespResult<List<PrInvestorTableRes>> result = new TxnRespResult<>();
        List<PrInvestorTableRes> res;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            res = fundTableService.findInvestorByFundIdAndTenId(tenantId, fundId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

}
